function animateLeft (obj, target, callback) {
    clearInterval(obj.timer);
    obj.timer = setInterval(() => {
        let step = (target - obj.offsetLeft) / 10;
        step = step > 0 ? Math.ceil(step) : Math.floor(step);
        if (obj.offsetLeft === target) {
            clearInterval(obj.timer);
            if (callback) {
                callback();
            }
        }
        obj.style.left = obj.offsetLeft + step + 'px';
    }, 10)
}

function animateTop (obj, target, callback) {
    clearInterval(obj.timer);
    obj.timer = setInterval(() => {
        let step = (target - obj.offsetTop) / 10;
        step = step > 0 ? Math.ceil(step) : Math.floor(step);
        if (obj.offsetTop === target) {
            clearInterval(obj.timer);
            if (callback) {
                callback();
            }
        }
        obj.style.top = obj.offsetTop + step + 'px';
    }, 10)
}

function animateYPage (obj, target, callback) {   // 让页面滚动上下滚动
    // console.log('触发了')
    clearInterval(obj.timer);
    obj.timer = setInterval(() => {
        // console.log('改造前'+window.pageYOffset)
        let step = (target - window.pageYOffset) / 10;
        step = step > 0 ? Math.ceil(step) : Math.floor(step);
        if (window.pageYOffset === target) {
            clearInterval(obj.timer);
            if (callback) {
                callback();
            }
        }
        window.scroll(0,window.pageYOffset + step);
        // console.log('改造后'+window.pageYOffset)
    }, 10)
}

function animateXPage (obj, target, callback) {   // 让页面滚动左右滚动
    clearInterval(obj.timer);
    obj.timer = setInterval(() => {
        let step = (target - window.pageXOffset) / 10;
        step = step > 0 ? Math.ceil(step) : Math.floor(step);
        if (window.pageXOffset === target) {
            clearInterval(obj.timer);
            if (callback) {
                callback();
            }
        }
        window.scroll(window.pageXOffset + step, 0);
    }, 10)
}

export  {
    animateLeft,
    animateTop,
    animateYPage,
    animateXPage
}

